<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 敏感词过滤
 *
 * @param  string
 * @return string
 */
function sensitive_words_filter($str)
{
    header('content-type:text/html;charset=utf-8');
    if (!$str) return '';
    $file = ROOT_PATH. 'public/static/plug/censorwords/CensorWords';
    $words = file($file);
    foreach($words as $word)
    {
        $word = str_replace(array("\r\n","\r","\n"," "), '', $word);
        if (!$word) continue;

        $ret = @preg_match("/$word/", $str, $match);
        if ($ret) {
            return $match[0];
        }
    }
    return '';
}

function getController(){
    return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_',think\Request::instance()->controller()));
}

function getModule(){
    return strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_',think\Request::instance()->module()));
}

function getUrl($key){
    return think\Url::build('admin/widget.images/index',['fodder'=>$key]);
}
function setbucketName(){
    return 'teaseyoulearn/';
}

function getKeyAttr($value,$returnType=true,$rep=''){
    if(!$value) return '';
    if(!$rep) $rep=setbucketName();
    $pic=$value;
    $value=parse_url($value);
    $value=isset($value['path']) ? substr($value['path'],1) : '';
    $data['key']=str_replace($rep,'',$value);
    $data['pic']=$pic;
    if($returnType){
        return $data;
    }else{
        return $data['key'];
    }
}

/**
 * 数据脱敏
 * @param    [type]           $string [description]
 * @param    integer          $start  [description]
 * @param    integer          $length [description]
 * @param    string           $re     [description]
 * @return   [type]                   [description]
 */
function desensitize($string, $start = 0, $length = 0, $re = '*'){
    if(empty($string) || empty($length) || empty($re)) return $string;
    $end = $start + $length;
    $strlen = mb_strlen($string);
    $str_arr = array();
    for($i=0; $i<$strlen; $i++) {
        if($i>=$start && $i<$end)
            $str_arr[] = $re;
        else
            $str_arr[] = mb_substr($string, $i, 1);
    }
    return implode('',$str_arr);
}

function p ($data)
{
    echo '<pre>';
    print_r($data);
    die;
}

function isMobile ($mobile)
{
    return preg_match('/^1[3456789]\d{9}$/' , $mobile);
}

function videoToAlbum($videoSrc , $title = '', $poster = '')
{
    return [
        'url'   => $videoSrc,
        'title' => $title,
        'poster'=> $poster,
        'type'  => 'video'
    ];
}

function imageToAlbum($imageSrc , $title = '')
{
    return [
        'url'   => $imageSrc,
        'title' => $title,
        'type'  => 'image'
    ];
}

function dateToWeek($date)
{
    $map = ['周日','周一','周二','周三','周四','周五','周六'];
    $index = date('w' , strtotime($date));
    $week = $map[$index] ?? '未知';
    return $week;
}

// 日期相差天数 dateDiffDays('2023-03-01' , '2023-12-31')
function dateDiffDays($d1 , $d2)
{
    $date1 = new DateTime($d1);
    $date2 = new DateTime($d2);
    $interval = $date1->diff($date2);
    return $interval->format('%a');
}

function html_to_entities($str, $charset = "utf-8")
{
   // 参数判断
    if(empty($str)) return "";
    
    // 1.将常用的预定义字符转为实体
    $new_str = htmlspecialchars($str, ENT_QUOTES, $charset);

    // 2.替换反斜杠
    $new_str = preg_replace("/\\\/", "&#092;", $new_str);

    // 3.替换斜杠
    $new_str = preg_replace("/\//", "&#47;", $new_str);
    
    return $new_str;
}

/**
 * 将实体转为HTML
 * @param string $str     需要处理的字符串
 * @return string
 */
function entities_to_html($str)
{
   // 参数判断
    if(empty($str)) return "";
    
    // 1.将实体转为预定义字符
    $new_str = htmlspecialchars_decode($str, ENT_QUOTES);

    // 2.替换反斜杠实体
    $new_str = str_replace("&#092;", "\\", $new_str);

    // 3.替换斜杠实体
    $new_str = str_replace("&#47;", "/", $new_str);
    
    return $new_str;
}

// 身份证号转生日
function idnumber2birthday($idnumber) {
    $birthday = '0000-00-00';
    if ( strlen($idnumber) == 18 ) {
        $year = substr($idnumber , 6, 4);
        $month = substr($idnumber , 10, 2);
        $day = substr($idnumber , 12, 2);

        $birthday = sprintf('%s-%s-%s' , $year, $month, $day);
    }
    return $birthday;
}